//
//  AdviceViewController.m
//  HuateVideo
//
//  Created by  on 12-5-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AdviceViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "CommonWebServices.h"
@interface AdviceViewController ()

@end

@implementation AdviceViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)blur{
    [tv1 resignFirstResponder];
    [tv2 resignFirstResponder];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
   
	// Do any additional setup after loading the view.
    self.view.backgroundColor=TOOLBAR_TINTCOLOR;
    bgview=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
    bgview.backgroundColor=[UIColor clearColor];
    bgview.tag=110;
    
    UIButton* assbut=[UIButton buttonWithType:UIButtonTypeCustom];
    assbut.frame=CGRectMake(0, 0, 320, 416);
    assbut.backgroundColor=[UIColor clearColor];
    [assbut addTarget:self action:@selector(blur) forControlEvents:UIControlEventTouchUpInside];
    [bgview addSubview:assbut];
    
    UILabel*la=[[UILabel alloc] initWithFrame:CGRectMake(15, 20, 30, 30)];
    la.text=@"*";
    la.backgroundColor=[UIColor clearColor];
    la.textColor=[UIColor redColor];
    la.textAlignment=UITextAlignmentCenter;
    la.font=[UIFont boldSystemFontOfSize:40];
    [bgview addSubview:la];
    [la release];

    UILabel* ll=[[UILabel alloc] initWithFrame:CGRectMake(50, 10, 200, 30)];
    ll.textColor=[UIColor whiteColor];
    ll.text=LS(@"请描述你的建议、意见等");
    ll.backgroundColor=[UIColor clearColor];
    ll.font=[UIFont systemFontOfSize:16];
    [bgview addSubview:ll];
    [ll release];
    
    UILabel* b=[[UILabel alloc] initWithFrame:CGRectMake(320 -15-40, 10, 40, 30)];
    b.textAlignment=UITextAlignmentRight;
    b.text=LS(@"(必填)");
    b.backgroundColor=[UIColor clearColor];
    b.font=[UIFont systemFontOfSize:14];
    b.textColor=[UIColor redColor];
    [bgview addSubview:b];
    [b release];
    
    tv1=[[UITextView alloc] initWithFrame:CGRectMake(15, 40, 290, 80)];
    tv1.layer.cornerRadius=3;
    tv1.delegate=self;
    tv1.font=[UIFont systemFontOfSize:16];
    [bgview addSubview:tv1];
    [tv1 release];
    
    [self.view addSubview:bgview];
    [bgview release];
    
    
    UILabel* contract=[[UILabel alloc] initWithFrame:CGRectMake(15, 150, 280, 30)];
    contract.backgroundColor=[UIColor clearColor];
    contract.textColor=[UIColor whiteColor];
    contract.text=LS(@"你的联系方式(QQ、邮箱、电话)");
    contract.font=[UIFont systemFontOfSize:16];
    [bgview addSubview:contract];
    [contract release];
    
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"QQ",LS(@"邮箱"),LS(@"电话"),nil]];
    segmentControl.frame = CGRectMake(15, 180, 180, 30);
    segmentControl.tintColor = TOOLBAR_TINTCOLOR;
	[segmentControl setSegmentedControlStyle:UISegmentedControlStyleBar];
	[segmentControl setSelectedSegmentIndex:0];
	[segmentControl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
	[bgview addSubview:segmentControl];
	[segmentControl release];
    typenum = @"1";
    
    UILabel* b1=[[UILabel alloc] initWithFrame:CGRectMake(320 -15-40, 150, 40, 30)];
    b1.textAlignment=UITextAlignmentRight;
    b1.text=LS(@"(选填)");
    b1.backgroundColor=[UIColor clearColor];
    b1.font=[UIFont systemFontOfSize:14];
    b1.textColor=[UIColor whiteColor];
    [bgview addSubview:b1];
    [b1 release];

    tv2=[[UITextView alloc] initWithFrame:CGRectMake(15, 222, 290, 33)];
    tv2.layer.cornerRadius=3;
	tv2.contentInset = UIEdgeInsetsZero;
    tv2.delegate=self;
    tv2.font=[UIFont systemFontOfSize:16];
    [bgview addSubview:tv2];
    [tv2 release];
    
    UIButton* submit=[UIButton buttonWithType:UIButtonTypeCustom];
    submit.frame=CGRectMake(15, 270, 75, 30);
    [submit setBackgroundImage:[UIImage imageNamed:@"submit.png"] forState:UIControlStateNormal];
    [submit setTitle:LS(@"提交") forState:UIControlStateNormal];
    [submit setTitleColor:LIGHT_COLOR forState:UIControlStateNormal];
    [submit addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    [bgview addSubview:submit];
    
    UIBarButtonItem* right=[[UIBarButtonItem alloc] initWithTitle:LS(@"提交") style:UIBarButtonItemStylePlain target:self action:@selector(submit)];
    self.navigationItem.rightBarButtonItem=right;
    [right release];
}

- (void)segmentChanged:(UISegmentedControl *)sender
{
	typenum = [[NSString stringWithFormat:@"%d",sender.selectedSegmentIndex + 1] retain];
}

-(void)submit{
	[tv2 resignFirstResponder];
    [tv1 resignFirstResponder];
		
    if ([tv1.text length]) {
        NSString *descript = tv1.text;
        NSString *information = tv2.text;
        if ([tv2.text length]) {
			NSString *regexString ;
			
			if ([typenum isEqualToString:@"2"]) {
				regexString = @"[A-Za-z0-9_.%]+@[A-Za-z0-9_.%]+\\.[A-Za-z]";
			}else if ([typenum isEqualToString:@"1"]) {
				regexString = @"\\d{4,12}";
			}else {
				regexString = @"((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)";
			}
			NSPredicate* valtest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexString]; 
			BOOL ret = [valtest evaluateWithObject:tv2.text];
			if (!ret) {
				UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"提示")  
																	message:LS(@"请正确填写联系方式")
																   delegate:self 	
														  cancelButtonTitle:LS(@"确定")
														  otherButtonTitles:nil];		
				[alertview show];		
				[alertview release];
				return;
			}
		}
        CommonWebServices *webServ = [[CommonWebServices alloc] init];
        webServ.delegate = self;
        NSMutableDictionary *parma = [NSMutableDictionary dictionary];
        [parma setValue:typenum forKey:@"typenum"];
        [parma setValue:descript forKey:@"descript"];
        [parma setValue:information forKey:@"information"];
        [webServ startRequestWithMode:@"Default" Action:@"AddFeedBack" Parma:parma Class:@"AddFeedBackResponse" RootXML:@"AddFeedBackResponse" resultIsArray:NO];
        [webServ release];
    }
    else {
        UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"提示")  
                                                            message:LS(@"意见或建议不能为空")
                                                           delegate:self 	
                                                  cancelButtonTitle:LS(@"确定")
                                                  otherButtonTitles:nil];		
        [alertview show];		
        [alertview release];
    }
	
	
    
}

- (void)onCommonWebServicesWithCustomClass:(id)object {
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"提示")  
                                                        message:LS(@"建议已发出，感谢您的支持")
                                                       delegate:self 	
                                              cancelButtonTitle:LS(@"确定")
                                              otherButtonTitles:nil];		
    [alertview show];		
    [alertview release];
}

- (void)onCommonWebServicesFaild
{
    UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"连接服务器失败")  
                                                        message:LS(@"请检查网络连接和设置")
                                                       delegate:self 	
                                              cancelButtonTitle:LS(@"确定")
                                              otherButtonTitles:nil];		
    [alertview show];		
    [alertview release];
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if (textView==tv2) {
        CGPoint center=bgview.center;
        bgview.center=CGPointMake(center.x, center.y-150);
    }
    return YES;
}

-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if (textView==tv2) {
        CGPoint center=bgview.center;
        bgview.center=CGPointMake(center.x, center.y+150);
    }
    return YES;
}
- (void)viewDidUnload
{
    [super viewDidUnload];

    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
